﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.PM.Shell.Views
{
    /// <summary>
    /// Interaction logic for WorkItemView.xaml
    /// </summary>
    public partial class WorkItemView : UserControl
    {
        private List<WorkItem> _items;
        private WorkItemCategory _currentCategory;
        private ContextMenu _ctlContextMenu;

        public WorkItemView()
        {
            InitializeComponent();

            _ctlContextMenu = new System.Windows.Controls.ContextMenu();
            MenuItem menuItem = new MenuItem();
            menuItem.Header = "插入";
            _ctlContextMenu.Items.Add(menuItem);

            _currentCategory = new WorkItemCategory();
            _currentCategory.Id = Guid.NewGuid();
            _currentCategory.Code = "C0";
            _currentCategory.Caption = "第一类任务";
            _currentCategory.Level = 0;

            _items = new List<WorkItem>();
            for(int index = 0; index < 10; index++)
            {
                WorkItem item = new WorkItem();
                item.Id = Guid.NewGuid();
                item.Caption = "c"+index.ToString();
                item.CategoryCode = _currentCategory.Code;
                _items.Add(item);
            }
            BindToDataGrid();
        }

        private void _ctlDataGrid_InitializingNewItem(object sender, InitializingNewItemEventArgs e)
        {
            WorkItem newItem = e.NewItem as WorkItem;
            newItem.Id = Guid.NewGuid();
            newItem.Caption = "c" + DateTime.Now.Ticks;
            newItem.CategoryCode = "c001";
        }

        private void _ctlBtnBreakdown_Click_1(object sender, RoutedEventArgs e)
        {
            Dialogs.BreakdownTaskDlg dlg = new Dialogs.BreakdownTaskDlg();
            if (dlg.ShowDialog() == true)
            {
                WorkItemCategory parentWorkItemCategory = _currentCategory;
                _currentCategory = new WorkItemCategory();
                _currentCategory.Id = Guid.NewGuid();
                _currentCategory.Code = string.Format("{0}01", parentWorkItemCategory.Code);
                _currentCategory.Caption = string.Format("子类别{0}", _currentCategory.Code);
                _currentCategory.Level = parentWorkItemCategory.Level + 1;

                // 显示任务切分界面
                // 生成新的工作项列表
                List<WorkItem> parentItemList = _items;
                _items = new List<WorkItem>();
                for (int index = 0; index < parentItemList.Count; index++)
                {
                    WorkItem newItem = new WorkItem();
                    newItem.Caption = "";
                    newItem.Id = Guid.Empty;
                    newItem.Parent = parentItemList[index];
                    newItem.CategoryCode = _currentCategory.Code;
                    _items.Add(newItem);
                }

                BindToDataGrid();
            }
        }

        private void BindToDataGrid()
        {
            _ctlDataGrid.Columns.Clear();

            for (int index = 0; index <= _currentCategory.Level; index++)
            {
                if (index == _currentCategory.Level)
                {
                    DataGridTextColumn columnCaption = new DataGridTextColumn();
                    columnCaption.Header = "Caption";
                    columnCaption.Binding = new Binding("Caption");
                    _ctlDataGrid.Columns.Add(columnCaption);
                }
                else
                {
                    DataGridTextColumn columnCaption = new DataGridTextColumn();
                    columnCaption.Header = GetBindingPath(_currentCategory.Level - index);
                    columnCaption.Binding = new Binding(GetBindingPath(_currentCategory.Level - index));
                    columnCaption.IsReadOnly = true;
                    _ctlDataGrid.Columns.Add(columnCaption);
                }
            }
            _ctlDataGrid.ItemsSource = _items;
        }

        private string GetBindingPath(int depth)
        {
            StringBuilder builder = new StringBuilder();
            for (int index = 0; index < depth; index++)
            {
                builder.Append("Parent.");
            }
            builder.Append("Caption");
            return builder.ToString();
        }

        private void _ctlDataGrid_MouseRightButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            _ctlContextMenu.IsOpen = true;
        }

        private void _ctlBtnInsert_Click_1(object sender, RoutedEventArgs e)
        {
            WorkItem selectedWorkItem = _ctlDataGrid.SelectedItem as WorkItem;

            if (selectedWorkItem != null)
            {
                WorkItem newItem = new WorkItem();
                newItem.Caption = "";
                newItem.Id = Guid.Empty;
                newItem.Parent = selectedWorkItem.Parent;
                newItem.CategoryCode = _currentCategory.Code;

                for (int index = 0; index < _items.Count; index++)
                {
                    if (_items[index] == selectedWorkItem)
                    {
                        _items.Insert(index, newItem);
                        break;
                    }
                }
                this._ctlDataGrid.ItemsSource = null;
                BindToDataGrid();
            }
        }
    }
}
